package director;

import drama.MoAttack;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import roles.GeLi;
import roles.LiuDeHua;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Director {
    @Test
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

        //1.构造函数注入
/*        GeLi geli = new LiuDeHua();
        MoAttack moAttack = new MoAttack(geli);*/

        //2.属性注入（setter方法）
/*        MoAttack moAttack = new MoAttack();
        moAttack.setGeli(new LiuDeHua());*/


        //3.接口注入
/*        GeLi geli = new LiuDeHua();
        MoAttack moAttack = new MoAttack();
        moAttack.injectGeli(geli);*/


        //采用spring Bean思想实现自动装配
/*        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        MoAttack moAttack = context.getBean("MoAttack", MoAttack.class);
        moAttack.cityGateAsk();*/

        //采用Spring spEL进行依赖注入
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        MoAttack moAttack = context.getBean("MoAttack", MoAttack.class);
        moAttack.cityGateAsk();

        //注解方法实现Bean思想
/*        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        MoAttack moAttack = context.getBean("moAttack", MoAttack.class);
        moAttack.cityGateAsk();*/





        //通过反射获取类的class对象

        //方式一：
/*        Class c1 = Class.forName("drama.MoAttack");
//        System.out.println(c1.hashCode());
        //通过反射调用普通方法(动态的创建对象)实现注入
        GeLi geli = new LiuDeHua();
        MoAttack moAttack = (MoAttack) c1.newInstance();
        Method setGeli = c1.getDeclaredMethod("setGeli", GeLi.class);
        setGeli.invoke(moAttack,geli);
        moAttack.cityGateAsk();*/

        //方式二：
/*        MoAttack moAttack = new MoAttack();
        GeLi geli = new LiuDeHua();
        Class c2 = moAttack.getClass();
        Method setGeli = c2.getDeclaredMethod("setGeli", GeLi.class);
        setGeli.invoke(moAttack,geli);
        moAttack.cityGateAsk();*/
//        System.out.println(c2.hashCode());

        //方式三：
/*        Class c3 = MoAttack.class;
        System.out.println(c3.hashCode());*/

        //方式四：获取基本内置类型的包装类都有一个Type属性
/*        Class type = Integer.TYPE;
        System.out.println(type);*/

        //通过getsuperclass获取某个类的父类对象

    }
}
